package com.jd.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jd.entity.RoleEntity;
import com.jd.service.RoleService;
import com.jd.util.IdUtil;


@Controller
@RequestMapping("/roleController")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/getRole")
	@ResponseBody
	public ArrayList<RoleEntity> getRole(HttpServletRequest request){
		String userId = request.getSession().getAttribute("userId").toString();
		ArrayList<RoleEntity> roleList = roleService.getRole(userId);
		return roleList;
	}
	
	@RequestMapping("/details")
	@ResponseBody
	public RoleEntity details(HttpServletRequest request){
		String id = request.getParameter("id");
		RoleEntity roleEntity = roleService.details(id);
		return roleEntity;
		
	}
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpServletRequest request){
		String userId = request.getSession().getAttribute("userId").toString();
		String name = request.getParameter("name");
		String nickname = request.getParameter("nickname");
		String nameArea = request.getParameter("name_area");
		String lv = request.getParameter("lv");
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("nameArea", nameArea);
		map.put("userId", userId);
		Integer i=roleService.count(map);
		if(i==0){
			RoleEntity roleEntity = new RoleEntity();
			roleEntity.setId(IdUtil.getIdByUUId());
			roleEntity.setCreateBy(userId);
			roleEntity.setCreateTime(new Date());
			roleEntity.setUserId(userId);
			roleEntity.setDelflag("0");
			roleEntity.setName(name);
			roleEntity.setNameArea(nameArea);
			roleEntity.setLv(lv);
			roleEntity.setNickname(nickname);
			roleService.save(roleEntity);
			return "添加成功";
		}else{
			return "已存在角色";
		}
		
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String update(HttpServletRequest request){
		String userId = request.getSession().getAttribute("userId").toString();
		String name = request.getParameter("name");
		String id = request.getParameter("id");
		String nickname = request.getParameter("nickname");
		String nameArea = request.getParameter("name_area");
		String lv = request.getParameter("lv");
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("nameArea", nameArea);
		Integer i=roleService.count(map);
		if(i==0){
			RoleEntity roleEntity = new RoleEntity();
			roleEntity.setId(id);
			roleEntity.setUpdateBy(userId);
			roleEntity.setUpdateTime(new Date());
			roleEntity.setUserId(userId);
			roleEntity.setDelflag("0");
			roleEntity.setName(name);
			roleEntity.setNameArea(nameArea);
			roleEntity.setLv(lv);
			roleEntity.setNickname(nickname);
			roleService.update(roleEntity);
			return "添加成功";
		}else{
			return "已存在角色";
		}
	}
}
